package com.secsbrain.cloudconsole.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.secsbrain.cloudconsole.remote.SsoRemoteService;
import com.secsbrain.cmdb.client.model.AppDetail;
import com.secsbrain.common.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.secsbrain.cloudconsole.remote.CmdbRemoteService;
import com.secsbrain.cloudconsole.remote.DeployRemoteService;
import com.secsbrain.cmdb.client.iface.AppInfoService;
import com.secsbrain.cmdb.client.model.AppTypeDto;
import com.secsbrain.cmdb.client.model.SystemTypeEnums;
import com.secsbrain.common.ErrorCodeEnum;
import com.secsbrain.common.ResponseVo;
import com.secsbrain.deploy.client.model.BuildModelDto;

@RestController
@RequestMapping("/deploy")
public class DeployController {
    
    private static final Logger logger = LoggerFactory.getLogger(DeployController.class);
    
    @Autowired
    private SsoRemoteService userServiceFacade;
    
    @Autowired
    private AppInfoService appInfoService;
    
    @Autowired
    private DeployRemoteService buildModelService;
    
    @Autowired
    private CmdbRemoteService cmdbRemoteService;
    
    
    /**
     * @apiVersion 1.0.0
     * @api {GET} /deploy/devList 获取开发/质量列表
     * 
     * @apiGroup Deploy
     * 
     * @apiSuccess {Object[]} data         数据列表
     * @apiSuccess {Number}   data.id   	      用户ID
     * @apiSuccess {Number}   data.realname   用户姓名
     * 
     */
    @RequestMapping(value = "/devList", method = RequestMethod.GET)
    public ResponseResult<?> queryDevList() {
		return userServiceFacade.getDevelopers();
    }
    
    /**
     * @apiVersion 1.0.0
     * @api {GET} /deploy/appTypes 获取应用类型
     * 
     * @apiGroup Deploy
     * 
     * @apiSuccess {String[]} data      数据列表
     * 
     */
    @RequestMapping(value = "appTypes", method = RequestMethod.GET)
    public ResponseResult<?> appTypes() {
        return new ResponseResult(SystemTypeEnums.values());
    }
    
    /**
     * @apiVersion 1.0.0
     * @api {GET} /deploy/appSort 获取应用分类
     * 
     * @apiGroup Deploy
     * 
     * @apiSuccess {String[]} data      数据列表
     * 
     */
    @RequestMapping(value = "/appSort", method = RequestMethod.GET)
    public ResponseResult<?> appSort() {
		ResponseVo<List<AppTypeDto>> appTypeVo=appInfoService.queryAppType();
		if(!appTypeVo.isSuccess()){
			return new ResponseResult(ErrorCodeEnum.ERROR_COMMON_SYSTEM.getCode(),appTypeVo.getMsg());
		}
		return new ResponseResult(appTypeVo.getData());
    }
    
    /**
     * @apiVersion 1.0.0
     * @api {GET} /deploy/appList 获取所有接入自动化的应用列表
     * 
     * @apiGroup Deploy
     * 
 	 * @apiSuccess {Object[]} data         数据列表
     * @apiSuccess {Number}   data.id       应用ID
     * @apiSuccess {Number}   data.name     应用名称
     * 
     */
    @RequestMapping(value = "appList", method = RequestMethod.GET)
    public ResponseResult<?> appList() {
    	ResponseVo<List<BuildModelDto>> buildModelVo=buildModelService.queryAllBuildModel();
    	if(!buildModelVo.isSuccess()){
			return new ResponseResult(ErrorCodeEnum.ERROR_COMMON_SYSTEM.getCode(),buildModelVo.getMsg());
    	}
    	List<Long> appIdList=new ArrayList<>();
    	for(BuildModelDto buildModelDto:buildModelVo.getData()){
			appIdList.add(buildModelDto.getAppId());
    	}
		Long[] appIdArry = appIdList.toArray(new Long[appIdList.size()]);
		ResponseVo<List<AppDetail>> appDetailVo=cmdbRemoteService.queryAppDetailByIds(appIdArry);
		if(!appDetailVo.isSuccess()){
			return new ResponseResult(ErrorCodeEnum.ERROR_COMMON_SYSTEM.getCode(),appDetailVo.getMsg());
		}
		return new ResponseResult(appDetailVo.getData());
    }
    
    /**
     * @apiVersion 1.0.0
     * @api {GET} /deploy/bootAppList 获取未接入分组发布的boot应用
     * 
     * @apiGroup Deploy
     * 
 	 * @apiSuccess {Object[]} data         数据列表
     * @apiSuccess {Number}   data.id       应用ID
     * @apiSuccess {Number}   data.name     应用名称
     * 
     */
    @SuppressWarnings("rawtypes")
	@RequestMapping(value = "bootAppList", method = RequestMethod.GET)
    public ResponseResult<?> bootAppList() {
    	ResponseVo<List<BuildModelDto>> buildModelVo=buildModelService.queryAllBuildModel();
    	if(!buildModelVo.isSuccess()){
			return new ResponseResult(ErrorCodeEnum.ERROR_COMMON_SYSTEM.getCode(),buildModelVo.getMsg());
    	}
    	
    	List<Long> appIdList=new ArrayList<Long>();
    	for(BuildModelDto buildModelDto:buildModelVo.getData()){
    		if(SystemTypeEnums.BOOT.toString().equals(buildModelDto.getSystemType())){
				Map map=JSON.parseObject(buildModelDto.getModelJson(), Map.class);
				Boolean isOpen=map.containsKey("switchflow")?(Boolean)map.get("switchflow"):null;
				if(!isOpen){
					appIdList.add(buildModelDto.getAppId());
				}
    		}
    	}
    	
		Long[] appIdArry = appIdList.toArray(new Long[appIdList.size()]);
		ResponseVo<List<AppDetail>> appDetailVo=cmdbRemoteService.queryAppDetailByIds(appIdArry);
		if(!appDetailVo.isSuccess()){
			return new ResponseResult(ErrorCodeEnum.ERROR_COMMON_SYSTEM.getCode(),appDetailVo.getMsg());
		}
		return new ResponseResult(appDetailVo.getData());
    }

}
